package com.ikas.ai.server.module.morningpaper.model.elements;

import com.ikas.ai.server.module.data.service.DorisYcDataService;
import com.ikas.ai.server.module.morningpaper.exception.MorningPaperException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class AbstractElementNeedsDorisYc extends MorningPaperTopElement{
    @Autowired
    protected DorisYcDataService dorisYcDataService;

    /**
     * 从dorisYcData表获取数据
     */
    protected <T> T getValueFromDorisYcTable(MorningPaperTopElement element, Class<T> clazz){
        String valueFromTable = dorisYcDataService.getValueFromYcTable(getMeteCode(element));
        return this.getT(clazz, valueFromTable);
    }

    /**
     * 获取当前Element的MeteCode，如果有的话
     * @param morningPaperTopElement
     * @return
     */
    protected String getMeteCode(MorningPaperTopElement morningPaperTopElement){
        String meteCode = this.getPoMap().get(morningPaperTopElement.getMorningPaperVariableName()).getMeteCode();
        if (StringUtils.isBlank(meteCode)){
            throw new MorningPaperException(String.format(MorningPaperException.meteIdOrMeteCodeNotExist, morningPaperTopElement.getMorningPaperVariableName()));
        }
        return meteCode;
    }


}
